package 反射;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.runtime.ObjectMethods;
import java.util.jar.Attributes.Name;

public class test {
	public static void main(String[] args) throws Exception
	{
		//1.
		Student s=new Student();
		Class c1=s.getClass();
		//2.
		Class c=Student.class;
		//3.
		
		Constructor[] con=c.getConstructors();//反射出public修饰的构造器
		Constructor[] con1=c.getDeclaredConstructors();//反射全部的构造器
		for(Constructor s1:con1)
		{
			System.out.println(s1);
		}
		Constructor conn=c.getDeclaredConstructor();//创建无参构造器对象
		Constructor conn1=c.getDeclaredConstructor
				(String.class,int.class);//创建有参构造器对象（public）
		Object student=conn1.newInstance("李明",18);
		System.out.println(student);
		//1.
		Method[] m=c.getDeclaredMethods();
		for(Method s1:m)
		{
			System.out.println("方法名称"+(String)s1.getName()
			+"修饰符"+s1.getModifiers());
		}
		Method m1=c.getDeclaredMethod("getName");
		System.out.println((String)m1.getName()+"  "+m1.getModifiers());
		//2.
		Field[] f=c.getDeclaredFields();
		for(Field d:f)
		{
			System.out.println("属性名称"+d.getName()+"属性类型"+d.getType());
		}
		Field f1=c.getDeclaredField("name");
		f1.setAccessible(true);
		f1.set(s,"liming");
		System.out.println(f1.get(s));
	}
}
